package main;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author WIN7
 */
public class Map{
    public static final int WEST = 0;
    public static final int NORTH = 1;
    public static final int EAST = 2;
    public static final int SOUTH = 3;
    private int direction;
    private String[] itemListName = 
    {
        "Canalave City",//0 
        "Black City", //1
        "Fusube City",//2
        "Canalave City",//3
        "Castelia City",//4
        "Castelia City", //5
        "Berry City",//6
        "Black Forest", //7
        "White Forest", //8
        "Red Forest",//9
    };
    private int[][] ItemLocationList = {
        //{20, 60}, 
        {5, 60},//0
        {60, 40}, //1
        {110, 50}, //2
        {170, 40},//3
        {190, 100},//4
        {171, 131},//5
        {130, 110},//6
        {70, 131},//7
        {65, 170},//8
        {172, 180}//9
    };
    private int[][] relationShipBetweenItems = 
    {
        {-1, -1, 1, -1},    //0
        {0, -1, 2, -1},     //1
        {1, -1, 3, 6},      //2
        {2, -1, -1, 4},     //3
        {6, 3, -1, 5},      //4
        {6, 4, -1, 9},      //5
        {7, 2, 4, 5},      //6
        {-1, -1, 6, 8},     //7
        {-1, 7, 9, -1},     //8
        {8, 5, -1, -1},     //9
    };
    private double aLine;
    private double bLine;
    private Item[] ItemList;
    
    private boolean going = false;
    private Item currentItem = null;
    private Item sourceItem;
    private Item desItem;
    
    private Character character;
    private Sprite backgroundSprite;
    private LayerManager layerManager;
    public Map()
    {
        int nItem = ItemLocationList.length;
        try
        {
            backgroundSprite = new Sprite(Image.createImage("/MainMap.png"));
        }catch(Exception ex)
        {
            ex.printStackTrace();
            return;
        }
        ItemList = new Item[nItem];
        Pokemon bubasasPokemon = new Pokemon("Bubasas", "/Bubasas.png", 41, 41, 6, 4, 2, 2);
        bubasasPokemon.getSprite().setPosition(0, 0);
        character = new Character("/Character.png", 22, 27, bubasasPokemon);
        layerManager = new LayerManager();
        layerManager.append(character.getSprite());
        for(int i=0; i<nItem; i++)
        {
            if(i < 7)
                ItemList[i] = new City(itemListName[i], ItemLocationList[i][0], ItemLocationList[i][1]);
            else
                ItemList[i] = new Forest(itemListName[i], ItemLocationList[i][0], ItemLocationList[i][1]);
            layerManager.append(ItemList[i].getSprite());
        }
        layerManager.append(backgroundSprite);
        character.setPosition(ItemList[0].getX(), ItemList[0].getY());
        currentItem = ItemList[0];
        setNeighborItems();
    }
    
    public void calculateDirection()
    {
        int Xa, Xb;
        int Ya, Yb;

        Xa = sourceItem.getX();
        Ya = sourceItem.getY();
        Xb = desItem.getX();
        Yb = desItem.getY();

        if(Xa==Xb)
            aLine = 0;
        else
            aLine = (Ya - Yb)*1.0/(Xa - Xb);
        bLine = Ya - aLine*Xa;
    }
 
    public void updateState()
    {
        if(going)
        {
            int x, y;
            x = character.getX();
            y = character.getY();
            switch(direction)
            {
                case WEST:
                {
                    character.moveLeft();
                    //Move to next Item
                    calculateDirection();
                    x -= 2;
                    y = (int)(aLine*x + bLine);
                    character.setPosition(x, y);
                }
                    break;
                case EAST:
                {
                    character.moveRight();
                    //Move to next Item
                    calculateDirection();
                    x += 2;
                    y = (int)(aLine*x + bLine);
                    character.setPosition(x, y);
                }
                    break;
                case NORTH:
                {
                    character.moveUp();
                    //Move to next Item
                    calculateDirection();
                    y -= 2;
                    x = (int)((y - bLine)/aLine);
                    character.setPosition(x, y);
                }
                    break;
                case SOUTH:
                {
                    character.moveDown();
                    //Move to next Item
                    calculateDirection();
                    y += 2;
                    x = (int)((y - bLine)/aLine);
                    character.setPosition(x, y);
                }
                    break;
                default:
                    break;
            }
            if(character.arrive(desItem))
            {
                going = false;
                currentItem = desItem;
            }
        }else
        {
            character.moveDown();
        }
        
    }
    public void animation()
    {
        character.animation();
    }
 
    public void paint(Graphics g)
    {
        int nItem = ItemLocationList.length;
        layerManager.paint(g, 0, 0);
        for(int i=0; i<nItem-1; i++)
        {
            if(ItemList[i] == currentItem)
                g.setColor(255, 0, 255);
            else
                g.setColor(200, 0, 0);
            ItemList[i].drawInfo(g);
            g.setColor(0, 255, 0);
            for(int j=0; j<4; j++)
            {
                Item neighborItem = ItemList[i].getNeighborItems()[j];
                if(neighborItem == null)
                    continue;
                g.drawLine(ItemList[i].getX() + 10, ItemList[i].getY()+ 10, neighborItem.getX()+ 10, neighborItem.getY()+ 10);
            }
        }
        if(ItemList[nItem - 1] == currentItem)
                g.setColor(255, 0, 255);
            else
                g.setColor(200, 0, 0);
        ItemList[nItem - 1].drawInfo(g);
//        g.setColor(200, 200, 0);
//        Font font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN,Font.SIZE_SMALL);
//        g.setFont(font);
        //String guide = "Press right key to move \nto next Item\nPress left key to move \nto previous Item";
        //g.drawString(guide, 0, backgroundSprite.getHeight() + 20, Graphics.BASELINE|Graphics.LEFT);
    }
    
    public void goToEasternItem()
    {
        if(going)
            return;
        direction = EAST;
        sourceItem = currentItem;
        desItem = sourceItem.getEasternItem();
        if(desItem == null)
            going = false;
        else
            going = true;
    }
    
    
    public void goToWesternItem()
    {
        if(going)
            return;
        direction = WEST;
        sourceItem = currentItem;
        desItem = sourceItem.getWesternItem();
        if(desItem == null)
            going = false;
        else
            going = true;
    }
    
    public void goToNorthernItem()
    {
        if(going)
            return;
        direction = NORTH;
        sourceItem = currentItem;
        desItem = sourceItem.getNorthernItem();
        if(desItem == null)
            going = false;
        else
            going = true;
    }
    
    public void goToSouthernItem()
    {
        if(going)
            return;
        direction = SOUTH;
        sourceItem = currentItem;
        desItem = sourceItem.getSouthernItem();
        if(desItem == null)
            going = false;
        else
            going = true;
    }
    
    
    public void chooseItem()
    {
        //showStoryModeMenuOption();
    }
    
    public Item getCurrentItem()
    {
        return currentItem;
    }
    
    public Character getCharacter()
    {
        return character;
    }
    
    private void setNeighborItems()
    {
        for(int i=0; i<relationShipBetweenItems.length; i++)
        {
            Item[] neighborItems = new Item[4];
            for(int j=0; j<4; j++)
            {
                if(relationShipBetweenItems[i][j] == -1)
                    continue;
                neighborItems[j] = ItemList[relationShipBetweenItems[i][j]];
            }
            ItemList[i].setNeighborItems(neighborItems);
        }
    }
    
}
